package uk.ac.ebi.bioinvindex.model.impl;

import uk.ac.ebi.bioinvindex.model.impl.term.CharacteristicImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.CharacteristicValueImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.MaterialRoleImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.Parameter;
import uk.ac.ebi.bioinvindex.model.impl.term.ParameterValue;
import uk.ac.ebi.bioinvindex.model.impl.term.UnitValue;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 12, 2007
 */
@Entity
@Table(name = "material")
public class MaterialImpl extends AnnotatableImpl {

	private Collection<CharacteristicValueImpl> characteristicValues = new ArrayList<CharacteristicValueImpl>();

	private String name;

	private MaterialRoleImpl type;

	public MaterialImpl(String name, MaterialRoleImpl type) {
		this.name = name;
		this.type = type;
	}

	protected MaterialImpl() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(targetEntity = uk.ac.ebi.bioinvindex.model.impl.term.CharacteristicValueImpl.class,
			mappedBy = "material", cascade = CascadeType.ALL)
	public Collection<CharacteristicValueImpl> getCharacteristicValues() {
		return characteristicValues;
	}

	protected void setCharacteristicValues(Collection<CharacteristicValueImpl> values) {
		this.characteristicValues = values;
	}

	public void addCharacteristicValue(CharacteristicValueImpl value) {
		characteristicValues.add(value);
		value.setMaterial(this);
	}

	public boolean removeCharacteristicValue(CharacteristicValueImpl value) {
		return characteristicValues.remove(value);
	}

	@ManyToOne(targetEntity = MaterialRoleImpl.class)
	@JoinColumn(name = "type", nullable = false)
	public MaterialRoleImpl getType() {
		return type;
	}

	public void setType(MaterialRoleImpl type) {
		this.type = type;
	}

	@Override
	public String toString ()
	{
		StringBuilder result = new StringBuilder (
			"MaterialImpl { id =  " + this.getId() + " name = " + this.getName ()
		);
	  result.append ( "  characteristics: [ "  );
	  String separator = "";
	  for ( CharacteristicValueImpl characteristic: this.getCharacteristicValues () ) {
	  	result.append ( separator );
	  	CharacteristicImpl characteristicType = characteristic.getType ();
	  	String characteristicName = characteristicType.getValue ();
	  	result.append ( characteristicName != null && characteristicName.length () != 0 ? characteristicName : "?" );
	  	result.append ( " = " + characteristic.getValue () );
	  	UnitValue unit = characteristic.getUnit ();
	  	if ( unit != null ) result.append ( unit.getValue () );
	  	separator = ", ";
	  }
	  result.append ( "] }" );
		return result.toString ();
	}



}
